<cftry>
<cfoutput>
<cfif #form.amount# lte 0>

<cflocation url="../creditcard.cfm?response=The Amount cannot be $0.00">


<cfelse>




<cfif not isdefined("session.ReceiptNum")>
<cfinclude template="../createinvoice.cfm">

</CFIF>


<cfinclude template="../addtocart.cfm">

<cfset parmlist=URLEncodedFormat('un~demo123|pswd~demo123|method~processtranx|tranxtype~sale|terms~Y|CC~#form.cc#|expmnth~#form.mymonth#|expyr~#form.myyear#|amount~#form.amount#|baddress~#getparent.BillAddress#|bzip~#getparent.BillZip#|invoice~#session.ReceiptNum#|TEST~Y')>


<cfhttp url="https://PayTrace.com/API/default.pay?parmList=#parmlist#"></cfhttp>
<!---RESPONSE~101. Your transaction was successfully approved.|TRANSACTIONID~18658377|APPCODE~TAS591|APPMSG~ NO MATCH |AVSRESPONSE~No Match|CSCRESPONSE~|--->

  
  <cfset checkcust = "#cfhttp.filecontent#">

<!--- <cfset checkone = listGetAt(checkcust, 2, "~")>--->
<cfif NOT ListLen(checkcust,"~") GTE 2>
	An Error Occured<br>
	checkcust = #checkcust#
	<cfabort>
</cfif>
  <cfset checktwo = listGetAt(checkcust, 2, "~")>
  
  <cfset parcartid = listGetAt(checkcust, 2, "|")>
  <cfset cartid = listGetAt(parcartid, 2, "~")>
  
 <cfset checkthree = listGetAt(checktwo, 1,".")>
  <cfset checkfour = listGetAt(checktwo, 2,".")>

<!---#checkcust#<br />
Cart ID: #cartid#
  
Code: #checkthree#--->
  
  
  
<cfset today = dateformat(now(), "mm/dd/yyyy")>
<cfset mytime = timeformat(now(), "hh:mm:ss")>

<cfset checkcard = "#form.cc#">




<cfset lastfour =  right(checkcard,4)>

<CFQUERY NAME="getitemssum" DATASOURCE="#dsn#">
select sum(Price) as total
from tbltransactions
where ReceiptNum = #session.ReceiptNum#
</CFQUERY>

 

<!---<cfset mychange = #form.amount# - #getitemssum.total#>--->

<cfset myblance = 0>
<cfset mychange = 0>



<cfparam name="cartid" default="">
<cfparam name="checkthree" default="">
<cfparam name="checkfour" default="">





<CFQUERY NAME="getcodes" DATASOURCE="#Datasource#">
select *
from tblresponsecodes
where Code = #checkthree#
</CFQUERY>

<cfif #checkthree# is 101 or #checkthree# is 104>
<cfset session.cartid = "#cartid#">

<cfset session.response = "#checkthree# #checkfour#">


<CFQUERY NAME="getorders" DATASOURCE="#dsn#">
select *
from tblorders
where ReceiptNum = #session.ReceiptNum#
</CFQUERY>


<cfquery datasource="#dsn#">
update tblorders
set Amount = #amount#,
LastFour = #lastfour#,
FirstName = '#cc_firstName#',
LastName = '#cc_lastName#',
Paytype = 'Credit Card',
Credit = #amount#,
TransType = 'SALE',
cartid = #session.cartid#,
pin = #getorders.pin#
where ReceiptNum = #session.ReceiptNum# 
</cfquery> 




<CFQUERY DATASOURCE="#dsn#">
INSERT INTO tblpayments (Amount, ReceiptNum, TransDate, ReportTime, ReportDate, PaymentType, PIN, Change, username, amountpaid, Balance, TransType, Credit)
VALUES (
#Amount#,
#session.ReceiptNum#,
<cfqueryparam cfsqltype="cf_sql_timestamp" value="#today#">,
'#mytime#',
'#today#',
'Credit Card',
<CFIF #getorders.DODAAC# EQ "">NULL<cfelse>#getorders.DODAAC#</CFIF>,
0,
'#session.username#',
#Amount#,
<cfif #myblance# gte 1>#myblance#<cfelse>0</cfif>,
'SALE',
#Amount#
 
)
</CFQUERY>




<cflocation url="../thankyou.cfm?response=#checkthree# #checkfour#&code=#checkthree#&amount=#amount#">


<cfelse>


<cflocation url="../creditcard.cfm?response=#checkthree# #checkfour#&code=#checkthree#">
</cfif>
  
</cfif>
</cfoutput>
<cfcatch type="Any">
Error Occured.
</cfcatch>
</cftry>